package com.heatup.apt.management.util;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DocumentHelper {

	private static Log log = LogFactory
	.getLog(DocumentHelper.class);
	/**
	 * 将obj对象的所有get方法的方法名去掉get和方法的返回值进行封装
	 * @param obj
	 * @return
	 */
    public static Map<String, Object> getGetMethodInfo(Object obj) {
    	Map<String, Object> result = new HashMap<String, Object>();
    	Class cls = obj.getClass();
    	Method[] methods = cls.getMethods();
    	for (int i = 0; i < methods.length; i++) {
			if (isGetMethod(methods[i])) {
				try {
					String key = getFieldName(methods[i]);
					Object value = methods[i].invoke(obj, new Object[0]);
					if (value == null) {
						value = "";
					}
					result.put(key, value);
				} catch (Exception e) {
					log.error(e.getMessage(),e);
				}
			}
		}
    	return result;
    }

    /**
     * 将obj对象的所有公有get方法的名字去掉get字符串后作为列名列表返回
     * @param obj Map对象或者Bean
     * @return
     */
    public static List<String> getKeysFromObject(Object obj) {
    	List<String> list = new ArrayList<String>();
    	if (obj instanceof Map) {
    		Map<String, Object> map = (Map<String, Object>) obj;
    		Object[] keys = map.keySet().toArray();
    		for (int i = 0; i < keys.length; i++) {
				list.add(keys[i].toString());
			}
    	} else {
    		Class cls = obj.getClass();
        	Method[] methods = cls.getMethods();
        	for (int i = 0; i < methods.length; i++) {
    			if (isGetMethod(methods[i])) {
					list.add(getFieldName(methods[i]));
    			}
    		}
    	}
		return list;
	}

    /**
     * 调用set方法设置值
     * @param obj
     * @param keys
     * @param values
     */
    public static void invokeSetMethods(Object obj, String[] keys, String[] values) {
    	Method[] methods = obj.getClass().getMethods();
    	for (int i = 0; i < keys.length; i++) {
			for (int j = 0; j < methods.length; j++) {
				if (isSetMethod(methods[j])) {
					if (keys[i] != null && keys[i].equalsIgnoreCase(getFieldName(methods[j]))) {
						Class[] clss =  methods[j].getParameterTypes();
						if (clss != null && clss.length > 0) {
							Object[] params = getParams(clss[0], values[i]);
							try {
								methods[j].invoke(obj, params[0]);
							} catch (Exception e) {

								log.error(e.getMessage(),e);
							}
						}
					}
				}
			}
		}
    }

    /**
     * 判断方法是否为get方法
     * @param method
     * @return
     */
    private static boolean isGetMethod(Method method) {
    	if (method.getName().startsWith("get")
    			&& (method.getModifiers() & Modifier.PUBLIC) == 1) {
    		return true;
    	}
    	return false;
    }

    /**
     * 判断方法是否为set方法
     * @param method
     * @return
     */
    private static boolean isSetMethod(Method method) {
    	if (method.getName().startsWith("set")
    			&& (method.getModifiers() & Modifier.PUBLIC) == 1) {
    		return true;
    	}
    	return false;
    }

    /**
     * 获取get/set方法所对应的字段名
     * @param getMethod
     * @return
     */
    private static String getFieldName(Method method) {
    	String methodName = method.getName();
    	//截取get字符串后面的所有字符串
    	String fieldName = methodName.substring(3);
    	if (fieldName != null && fieldName.length() > 0) {
    		//将字段名的首字符转换成小写
    		fieldName = convertStartCharToLowerCase(fieldName);
    	}
    	return fieldName;
    }

    /**
     * 将字符串的首字符转换成小写
     * @param str
     * @return
     */
    private static String convertStartCharToLowerCase(String str) {
    	String start = str.charAt(0) + "";
    	start = start.toLowerCase();
    	String end = "";
    	if (str.length() > 1) {
    		end = str.substring(1);
    	}
    	return start + end;
    }

    /**
     * 将字符串的首字符转换成大写
     * @param str
     * @return
     */
    public static String convertStartCharToUpperCase(String str) {
    	String start = str.charAt(0) + "";
    	start = start.toUpperCase();
    	String end = "";
    	if (str.length() > 1) {
    		end = str.substring(1);
    	}
    	return start + end;
    }

    /**
     * 将字符串转换成Object数组
     * @param obj 方法名
     * @param value 字符串值
     * @return
     */
    public static Object[] getParams(Class cls, String value) {
    	Object[] objs = null;
    	if (value == null || value.length() < 1) {
    		objs = new Integer[1];
    		objs[0] = null;
    		return objs;
    	}
    	if (cls == Integer.TYPE || cls == Integer.class) {
    		objs = new Integer[1];
    		objs[0] = Integer.parseInt(value);
    	} else if (cls == Boolean.TYPE || cls == Boolean.class) {
    		objs = new Boolean[1];
    		objs[0] = Boolean.parseBoolean(value);
    	} else if (cls == Short.TYPE || cls == Short.class) {
    		objs = new Short[1];
    		objs[0] = Short.parseShort(value);
    	} else if (cls == Long.TYPE || cls == Long.class) {
    		objs = new Long[1];
    		objs[0] = Long.parseLong(value.substring(0, value.indexOf(".")));
    	} else if (cls == Float.TYPE || cls == Float.class) {
    		objs = new Float[1];
    		objs[0] = Float.parseFloat(value);
    	} else if (cls == Double.TYPE || cls == Double.class) {
    		objs = new Double[1];
    		objs[0] = Double.parseDouble(value);
    	} else {
    		objs = new String[1];
    		objs[0] = value;
    	}
    	return objs;
    }
}
